/**
 * Created by wushanxin on 14-5-22.
 */

var log = require("../lib/log");
var config = require('../config');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');


var db = require("../lib/db");
var viruser=require('../models/viruser.js');
exports.index = function(request, response) {

    if(typeof(request.session.admin)=="undefined" || request.session.admin==null || request.session.admin.length==0)
    {
        response.send("<script>window.parent.location.href='login.html';</script>");
        return;
    }
    var method = request.method || '';
    var viruserdata = RUtil.viruserdata(request);
    var retdata = RUtil.retdata(request);
    var viruserdetail = RUtil.viruser(request);

    if(method.toUpperCase() === "GET") {


        viruserdata.retdata.nickname = request.session.admin.nickname;
        var extend = require('util')._extend;
        viruserdata.retdata.roleinfo = extend({}, request.session.roleinfo);
        viruserdata.retdata.userinfo = request.session.admin;
        viruser.selectByID(request.session.admin.id, function(error,results) {
            if((error)!== null)
            {
                // 数据库异常
                viruserdata.retdata.info = "载入失败：系统内部错误(错误码："+error.errno+")";
                viruserdata.retdata.success=false;
                response.render("modifypwd", viruserdata);

            }
            else if(results.length <= 0) {
                // 不存在，直接响应页面
                viruserdata.retdata.info = "没有填写个人资料!";
                viruserdata.retdata.success=false;
                response.render("modifypwd", viruserdata);
            } else {
                //存在数据将数据发到页面
                viruserdata.retdata.info = "个人资料提取成功!";
                viruserdata.retdata.success=true;
                viruserdata.viruser=results[0];
                console.log( "session:"+request.session.roleinfo.authnumber);
                console.log( "session:"+request.session.roleinfo.statnumber);
                var num=request.session.roleinfo.authnumber;
                viruserdata.retdata.roleinfo.authnumber=RUtil.getspecialauthlist( num);
                num=request.session.roleinfo.statnumber;
                viruserdata.retdata.roleinfo.statnumber=RUtil.getspecialstatlist( num);
                console.log( "retdata:"+viruserdata.retdata.roleinfo.authnumber);
                console.log( "retdata:"+viruserdata.retdata.roleinfo.statnumber);
                response.render("modifypwd", viruserdata);
            }
        });

    }

    else if(method.toUpperCase() === "POST") {

        if((crypto.createHash('md5').update(request.body.oldpwd).digest('hex').toLowerCase())!==request.session.admin.userpwd.toLowerCase())
        {
            retdata.info = "原始密码错误！";
            retdata.success=false;
            response.send("modifypwd", retdata);
            return;
        }
        viruserdetail.userpwd=crypto.createHash('md5').update((request.body.newpwd)).digest('hex');
        viruserdetail.companyname=request.body.companyname;
        viruserdetail.phonenumber=request.body.phonenumber;
        viruserdetail.email=request.body.email;
        viruserdetail.nickname=request.body.nickname;

        viruserdetail.id  =request.session.admin.id;
        viruser.updateAllInfo(viruserdetail, function(error,results) {
            if((error)!== null)
            {
                // 数据库异常
                retdata.info = "更新失败：系统内部错误(错误码："+error.errno+")";
                retdata.success=false;
                response.send("modifypwd", retdata);

            }
            else if(results.length <= 0) {
                // 不存在，直接响应页面
                retdata.info = "没有填写个人资料";
                retdata.success=false;
                response.send("modifypwd", retdata);
            } else {
                //存在数据将数据发到页面
                RUtil.writeoperlog(request,'更新个人信息','更新成功：'+JSON.stringify(viruserdetail));
                retdata.info = "更新成功";
                retdata.success=true;
                response.send("modifypwd", retdata);
            }
        });
        return;

    }


};


